#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>	/* Necessary because we use the proc fs */
#include "proc_file.h"

struct proc_dir_entry *proc_entry;
static char * procfs_name;

int procfile_read(char *buffer, char **buffer_location,
		off_t offset, int buffer_length, int *eof, void *data){
	int ret;

	printk(KERN_INFO "procfile_read (/proc/%s) called\n", procfs_name);

	/* 
	 * We give all of our information in one go, so if the
	 * user asks us if we have more information the
	 * answer should always be no.
	 *
	 * This is important because the standard read
	 * function from the library would continue to issue
	 * the read system call until the kernel replies
	 * that it has no more information, or until its
	 * buffer is filled.
	 */
	if (offset > 0) {
		/* we have finished to read, return 0 */
		ret  = 0;
	} else {
		/* fill the buffer, return the buffer size */
		ret = sprintf(buffer, "HelloWorld!\n");
	}

	return ret;
}

int procfile_initialize( struct PCI_CONFIG* config ){
	char * procfs_name = (char *) config->proc_file;
	proc_entry = create_proc_entry(procfs_name, 0644, NULL);

	if (proc_entry == NULL) {
		remove_proc_entry(procfs_name, &proc_root);
		printk(KERN_ALERT "Error: Could not initialize /proc/%s\n", procfs_name);
		return -ENOMEM;
	}

	proc_entry->read_proc	= procfile_read;
	proc_entry->owner	= THIS_MODULE;
	proc_entry->mode	= S_IFREG | S_IRUGO;
	proc_entry->uid		= 0;
	proc_entry->gid		= 0;
	proc_entry->size	= 37;

	printk(KERN_INFO "/proc/%s created\n", procfs_name);	
	return 0;	/* everything is ok */
}

